const loginInfo = {
  state: {
    accessToken: '',
    refreshToken: '',
    userInfo: {
      username: '',
      phone: '',
      id: -1,
      email: ''
    },
    menus: []
  },
  mutations: {
    login (state, data) {
      console.log('设置token')
      console.log(data)
      state.accessToken = data.token
      state.refreshToken = data.refresh_token
      localStorage.setItem('accessToken', state.accessToken)
      localStorage.setItem('refreshToken', state.refreshToken)
    },
    setUserInfo (state, data) {
      console.log('设置用户信息')
      console.log(data)
      state.userInfo = data
    },
    setMenus (state, data) {
      console.log('setMenus')
      console.log(data)
      state.menus = data
    }
  },
  actions: {
    login (context) {
      context.commit('login')
    }
  },
  getters: {
    getAccessToken: state => {
      if (state.accessToken) {
        return state.accessToken
      } else {
        if (localStorage.getItem('accessToken')) {
          let tokendata = {
            accessToken: localStorage.getItem('accessToken'),
            refreshToken: localStorage.getItem('refreshToken')
          }
          state.accessToken = tokendata.token
          state.refreshToken = tokendata.refresh_token
          return tokendata.accessToken
        } else {
          return ''
        }
      }
    },
    refreshToken: state => {
      if (state.refreshToken) {
        return state.refreshToken
      } else {
        if (localStorage.getItem('accessToken')) {
          let tokendata = {
            accessToken: localStorage.getItem('accessToken'),
            refreshToken: localStorage.getItem('refreshToken')
          }
          state.accessToken = tokendata.accessToken
          state.refreshToken = tokendata.refreshToken
          return tokendata.refreshToken
        } else {
          return ''
        }
      }
    },
    userInfo: state => {
      return state.userInfo
    }
  }
}
export default loginInfo
